﻿using SqlPerfomance.V2.DomainModel.Enums;
using SqlPerfomance.V2.Tests;
using SqlPerfomance.V2.Tests.Code;
using SqlPerfomance.V2.Tests.TestsCommand;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace SqlPerfomance.V2.WpfClient
{
    /// <summary>
    /// Interaction logic for RunTests.xaml
    /// </summary>
    public partial class RunTests : Window
    {
        private List<TestSettings> _settings;
        public RunTests()
        {
            _settings = new List<TestSettings>();
            InitializeComponent();
        }

        private void btn_add_setting_Click(object sender, RoutedEventArgs e)
        {
            int nodesCount = 0;
            int nestingLevel = 0;
            if(!int.TryParse(txt_nodes_count.Text, out nodesCount) || !int.TryParse(txt_nesting_level.Text, out nestingLevel))
            {
                MessageBox.Show("incorrect data", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            if(nodesCount <= 0 || nestingLevel <= 0)
            {
                MessageBox.Show("incorrect data", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            var item = new ListBoxItem();
            item.Content = string.Format("nodes count = {0}, nesting level = {1}", nodesCount, nestingLevel);
            lst_test_settings.Items.Add(item);
            _settings.Add(new TestSettings()
                {
                    NestingLevel = nestingLevel,
                    NodesCount = nodesCount
                });
        }

        private void btn_run_Click(object sender, RoutedEventArgs e)
        {
            if(!_settings.Any())
            {
                MessageBox.Show("settings list is empty. please add settings");
                return;
            }
            var testsManager = new MacroTest(GetCheckedCOnnectionString());
            var results = testsManager.Run(_settings).ToList();
            string textResult = GetResults(results);
            TestsResult res = new TestsResult(textResult);
            res.Visibility = System.Windows.Visibility.Visible;
        }

        private string GetResults(List<TestCommandResult> results)
        {
            StringBuilder sb = new StringBuilder();
            var groupedResults = results.GroupBy(t => t.HierarchyTestType)
                .Select(t => new { type = t.Key, data = t })
                .ToList();
            sb.AppendLine(GetCheckedCOnnectionString().ToString() + " connection");
            foreach (var type in groupedResults)
            {
                sb.AppendLine(type.type.ToString());
                sb.AppendLine("\tTests:");
                foreach (var item in type.data)
                {
                    sb.AppendFormat("\t\tSettings: nodes count = {0}, nesting level = {1}\n", item.Settings.NodesCount, item.Settings.NestingLevel);
                    foreach (var result in item.TestsResults)
                    {
                        if (result != null)
                        {
                            sb.AppendFormat("\t\t\t{0}, time = {1}\n", result.TestType.ToString(), result.TestTime.ToString());
                        }
                    }
                }
            }
            return sb.ToString();
        }

        private ConnectionStrings GetCheckedCOnnectionString()
        {
            return radio_sqlServer.IsChecked.HasValue && radio_sqlServer.IsChecked.Value ? ConnectionStrings.SqlServerConnectionString
                : ConnectionStrings.MySqlConnectionString;
        }

        private void btn_clear_settings_Click(object sender, RoutedEventArgs e)
        {
            lst_test_settings.Items.Clear();
            _settings.Clear();
        }
    }
}
